<?php
/**
 * 服务管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/21
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\CarServiceModel;
use think\facade\Request;

class ServiceController extends AdminBaseController
{
    /**
     * 文章列表
     */
    public function index(CarServiceModel $cs){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$cs ->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){

        $where=[];

        if(!empty($data["title"])){
            $where[]=["title",'like',"%{$data['title']}%"];
        }

        if(!empty($data["cat_id"])){
            $where[]=["cat_id","=",$data["cat_id"]];
        }

        if (!empty($data["status"]) && $data["status"] != -2) {
            $where[] = ["status", '=', $data['status'] - 1];
        }

        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }

        return $where;
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param ArticleModel $art
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($id=0,CarServiceModel $art){
        if(Request::isPost()){
            $data=Request::param();

            $status=$art->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=$art::get($id);

            $this->assign("info",$info);
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=CarServiceModel::get($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除
     */
    public function del(CarServiceModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }

}